package org.jfk.tbox.net.app;

import java.util.Date;

import org.jfk.core.utils.ByteUtils;
import org.jfk.tbox.net.TboxMsg;

public class DSRC_AbnormalAlarm {
	
	static final String appId = "0602";
	static final String[][] eventList = {
		{"0001","正前","追尾"},
		{"0002","右前","右车变道切入"},
		{"0004","右侧","交叉口右侧来车"},
		{"0008","右后","向右变道"},
		{"0010","正后","被追尾"},
		{"0020","左后","向左变道"},
		{"0040","左侧","交叉口左侧来车"},
		{"0080","左前","左车变道切入"}
		};
	static final String[][] eventList2 = {
		{"1","正前","追尾"},
		{"2","右前","右车变道切入"},
		{"3","右侧","交叉口右侧来车"},
		{"4","右后","向右变道"},
		{"5","正后","被追尾"},
		{"6","左后","向左变道"},
		{"7","左侧","交叉口左侧来车"},
		{"8","左前","左车变道切入"}
		};
	
	
	
	public class DSRC_AbnormalAlarmReq extends TboxMsg{
		
		byte alarmType;

		//定义远车和本车的参数
		double l_lng,l_lat,l_elev,r_lng,r_lat,r_elev;
		int l_heading,l_speed,r_heading,r_speed;
		int event;
		String eventDesc;
		
		int[] info = {0,0,0,0,0,0,0,0};
		
		public int[] getInfo(){
			return this.info;
		}
	
		public DSRC_AbnormalAlarmReq(){
			super.appId = DSRC_AbnormalAlarm.appId;
			super.msgId = 1;
		}
		
		public void loadData(byte[] data){
			super.loadData(data);
			byte[] _r_lat  = new byte[4] ,
					_r_lng  = new byte[4] ,
					_l_lat  = new byte[4] ,
					_l_lng = new byte[4] ,
					_time = new byte[4];
			
			byte[] _r_elev = new byte[2],
					_l_elev = new byte[2],
					_l_heading = new byte[2],
					_r_heading = new byte[2],
					_event = new byte[2];
			
			byte[] _appData = super.appData;

			int idx = 0;

			System.arraycopy(_appData, idx , _r_lat, 0 , _r_lat.length);
			_r_lat = ByteUtils.bytesInverse(_r_lat);
			idx = idx + _r_lat.length;
			r_lat = ByteUtils.byte4Tolong(_r_lat);

			System.arraycopy(_appData, idx , _r_lng, 0 , _r_lng.length);
			_r_lng = ByteUtils.bytesInverse(_r_lng);
			idx = idx + _r_lng.length;
			r_lng = ByteUtils.byte4Tolong(_r_lng);

			System.arraycopy(_appData, idx , _r_elev, 0 , _r_elev.length);
			_r_elev = ByteUtils.bytesInverse(_r_elev);
			idx = idx + _r_elev.length;
			r_elev = ByteUtils.byte2ToInt(_r_elev);

			System.arraycopy(_appData, idx , _r_heading, 0 , _r_heading.length);
			_r_heading = ByteUtils.bytesInverse(_r_heading);
			idx = idx + _r_heading.length;
			r_heading = ByteUtils.byte2ToInt(_r_heading);

			System.arraycopy(_appData, idx , _l_lat, 0 , _l_lat.length);
			_l_lat = ByteUtils.bytesInverse(_l_lat);
			idx = idx + _l_lat.length;
			l_lat = ByteUtils.byte4Tolong(_l_lat);

			System.arraycopy(_appData, idx , _l_lng, 0 , _l_lng.length);
			_l_lng = ByteUtils.bytesInverse(_l_lng);
			idx = idx + _l_lng.length;
			l_lng = ByteUtils.byte4Tolong(_l_lng);

			System.arraycopy(_appData, idx , _l_elev, 0 , _l_elev.length);
			_l_elev = ByteUtils.bytesInverse(_l_elev);
			idx = idx + _l_elev.length;
			l_elev = ByteUtils.byte2ToInt(_l_elev);

			System.arraycopy(_appData, idx , _l_heading, 0 , _l_heading.length);
			_l_heading = ByteUtils.bytesInverse(_l_heading);
			idx = idx + _l_heading.length;
			l_heading = ByteUtils.byte2ToInt(_l_heading);

			System.arraycopy(_appData, idx , _time, 0 , _time.length);
			_time = ByteUtils.bytesInverse(_time);
			idx = idx + _time.length;
			long time = ByteUtils.byte4Tolong(_time);
			super.eventCreateTime = new Date(0-(8*60*60*1000)+time*1000);

			System.arraycopy(_appData, idx , _event, 0 , _event.length);
			_event = ByteUtils.bytesInverse(_event);
			
			event = ByteUtils.byte2ToInt(_event);

		}		
		
		public byte[] getAppData(){
			return null;
		}		
		
		public byte[] getResp(){
			DSRC_AbnormalAlarmResp resp = new DSRC_AbnormalAlarmResp();
			super.initResp(resp);
			return resp.toBytes();
		}		
		
		public String toString(){
			return l_lat + " , " +
					l_lng + " , " +
					l_elev + " , " +
					l_heading + " , " +
					eventCreateTime + " , " +
					event + " , " +
					getEventDesc();
		}
		
		
		public String getEventDesc(){
			String eventDesc = "";
			String s = Integer.toBinaryString(event);
			String s0 = "00000000";
			s = s0.substring(0,8-s.length())+s;
			
			char[] c = s.toCharArray();
			for (int i = 1 ; i <= c.length ; i++){
				if (c[c.length-i] == '1'){
					for (int j = 0 ; j < eventList2.length ; j++){
						int idx = Integer.valueOf(eventList2[j][0]);
						if (idx == i){
							eventDesc = eventDesc + "告警信息来自"+eventList2[j][1]+"方,小心"+eventList2[j][2]+"。";
							info[j] = 1;
						}
					}
				}
			}

			return eventDesc;
		}		
	}

	public class DSRC_AbnormalAlarmResp extends TboxMsg{
		public DSRC_AbnormalAlarmResp(){
			super.appId = DSRC_AbnormalAlarm.appId;
			super.msgId = 2;
		}
		public byte[] getAppData(){
			byte[] b = {};
			return b;
		}		
		
		public byte[] getResp(){
			return null;
		}		
	}
	

	
	public static void main(String[] args){
	}
}
